//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_REGISTER_H__
#define __ELASTOS_REGISTER_H__

#include <regs.h>

//
// inline functions
//
INLINE void Cli()
{
    uint32_t  uStatus = 0;

    read_cp0_register($12, uStatus); // $12 : cp0_status
    uStatus &= (~0x1);
    write_cp0_register($12, uStatus);
}

INLINE void Sti()
{
    uint32_t  uStatus = 0;

    read_cp0_register($12, uStatus); // $12 : cp0_status
    uStatus |= 0x1;
    write_cp0_register($12, uStatus);
}

INLINE uint32_t SaveFlagsAndCli()
{
    uint32_t uFlags;

    read_cp0_register($12, uFlags);
    write_cp0_register($12, uFlags & (~0x00000001));

    return uFlags & 0x1;
}

INLINE void RestoreIF(uint_t uFlags)
{
    if (0x1 & uFlags) {
        Sti();
    }
}

INLINE void EnableCP1()
{
    uint32_t    uStatus;

    read_cp0_register($12, uStatus);
    uStatus |= ST0_CU1;
    write_cp0_register($12, uStatus);
}

INLINE void DisableCP1()
{
    uint32_t    uStatus;

    read_cp0_register($12, uStatus);
    uStatus &= ~ST0_CU1;
    write_cp0_register($12, uStatus);
}

#define IS_IN_USER_MODE(pContext)           \
    (0x10 == (pContext->cp0_status & 0x18))

#define IS_IN_KERNEL_MODE(pContext) \
        (!IS_IN_USER_MODE(pContext))

#endif //__ELASTOS_REGISTER_H__
